package cn.com.blueInfo.bpm.person.service.impl;

import cn.com.blueInfo.bpm.person.convert.UserAccountConvert;
import cn.com.blueInfo.bpm.person.entity.UserAccount;
import cn.com.blueInfo.bpm.person.repository.UserAccountRepository;
import cn.com.blueInfo.bpm.person.request.UserAccountDTO;
import cn.com.blueInfo.bpm.person.response.UserAccountVO;
import cn.com.blueInfo.bpm.person.service.UserAccountService;
import cn.com.blueInfo.core.config.CommonConfig;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工账户表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class UserAccountServiceImpl implements UserAccountService {

    @Autowired
    private UserAccountRepository userAccountRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private UserAccountConvert userAccountConvert;

    @Autowired
    private CommonConfig commonConfig;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 新增员工账户
     * @param userAccountDTO 员工账户参数
     */
    @Override
    public ResultInfo<Integer> addUserAccount(UserAccountDTO userAccountDTO) {
        if (commonConfig.isSupper()) {
            userAccountDTO.setTenantId(null);
            userAccountDTO.setApplicationId(null);
            userAccountDTO.setCreateUserId(null);
        }
        UserAccount userAccount = userAccountConvert.dtoToEntity(userAccountDTO);
        userAccount.setPassword(passwordEncoder.encode(userAccount.getPassword()));
        userAccount.setId(snowflakeIdGenerator.nextId());
        Integer addSize = userAccountRepository.createUserAccount(userAccount);
        return ResultInfo.<Integer>builder().data(addSize).message("员工账户成功新增 {} 条").build();
    }

    /**
     * 批量新增员工账户
     * @param userAccountDTOList 员工账户参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddUserAccount(List<UserAccountDTO> userAccountDTOList) {
        List<UserAccount> userAccountList = userAccountConvert.dtoToEntity(userAccountDTOList);
        for (UserAccount userAccount : userAccountList) {
            userAccount.setPassword(passwordEncoder.encode(userAccount.getPassword()));
            userAccount.setId(snowflakeIdGenerator.nextId());
        }
        Integer addSize = userAccountRepository.batchCreateUserAccount(userAccountList);
        return ResultInfo.<Integer>builder().data(addSize).message("员工账户成功新增 {} 条").build();
    }

    /**
     * 删除员工账户
     * @param userAccountId 员工账户ID
     */
    @Override
    public ResultInfo<Integer> removeUserAccount(Long userAccountId) {
        Integer removeSize = userAccountRepository.deleteUserAccount(userAccountId);
        return ResultInfo.<Integer>builder().data(removeSize).message("员工账户成功删除 {} 条").build();
    }

    /**
     * 批量删除员工账户
     * @param userAccountIdList 员工账户ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveUserAccount(List<Long> userAccountIdList) {
        Integer removeSize = userAccountRepository.batchDeleteUserAccount(userAccountIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("员工账户成功删除 {} 条").build();
    }

    /**
     * 更新部分员工账户
     * @param userAccountUpdatePartialDTO 员工账户参数
     */
    @Override
    public ResultInfo<Integer> modifyUserAccountPartial(
            UserAccountDTO.UpdatePartialDTO userAccountUpdatePartialDTO) {
        if (commonConfig.isSupper()) {
            userAccountUpdatePartialDTO.setCreateUserId(null);
        }
        UserAccount userAccount = userAccountConvert.dtoToEntity(userAccountUpdatePartialDTO);
        Integer modifySize = userAccountRepository.updateUserAccountPartial(userAccount);
        return ResultInfo.<Integer>builder().data(modifySize).message("员工账户成功更新 {} 条").build();
    }

    /**
     * 更新全部员工账户
     * @param userAccountDTO 员工账户参数
     */
    @Override
    public ResultInfo<Integer> modifyUserAccount(UserAccountDTO userAccountDTO) {
        if (commonConfig.isSupper()) {
            userAccountDTO.setTenantId(null);
            userAccountDTO.setApplicationId(null);
            userAccountDTO.setCreateUserId(null);
        }
        userAccountDTO.setPassword(passwordEncoder.encode(userAccountDTO.getPassword()));
        UserAccount userAccount = userAccountConvert.dtoToEntity(userAccountDTO);
        Integer modifySize = userAccountRepository.updateUserAccount(userAccount);
        return ResultInfo.<Integer>builder().data(modifySize).message("员工账户成功更新 {} 条").build();
    }

    /**
     * 查询一条员工账户
     * @param userAccountId 员工账户ID
     */
    @Override
    public ResultInfo<UserAccountVO> queryUserAccountById(Long userAccountId) {
        UserAccount userAccount = userAccountRepository.getUserAccountById(userAccountId);
        UserAccountVO userAccountVO = userAccountConvert.entityToVO(userAccount);
        return ResultInfo.<UserAccountVO>builder().data(userAccountVO).message("员工账户查询成功").build();
    }

    /**
     * 分页查询员工账户
     * @param userAccountQueryDTO 员工账户参数
     */
    @Override
    public ResultInfo<Page<UserAccountVO>> queryUserAccountPage(UserAccountDTO.QueryDTO userAccountQueryDTO) {
        Page<UserAccount> userAccountPage = userAccountRepository.getUserAccountPage(userAccountQueryDTO);
        Page<UserAccountVO> resultData = userAccountConvert.convertPage(userAccountPage);
        return ResultInfo.<Page<UserAccountVO>>builder()
                .data(resultData)
                .message("员工账户分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 员工账户 数量
     * @param userAccountQueryDTO 员工账户参数
     */
    @Override
    public ResultInfo<Long> queryUserAccountCount(UserAccountDTO.QueryDTO userAccountQueryDTO) {
        Long resultCount = userAccountRepository.getUserAccountCount(userAccountQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("员工账户数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 员工账户
     * @param userAccountQueryDTO 员工账户参数
     */
    @Override
    public ResultInfo<List<UserAccountVO>> queryUserAccountList(UserAccountDTO.QueryDTO userAccountQueryDTO) {
        List<UserAccount> userAccountList = userAccountRepository.getUserAccountList(userAccountQueryDTO);
        List<UserAccountVO> resultData = userAccountConvert.entityListToVoList(userAccountList);
        return ResultInfo.<List<UserAccountVO>>builder()
                .data(resultData)
                .message("员工账户分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
